<template>
	<view class="">
		<cu-navbar bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">授权登录</block>
		</cu-navbar>
		<view class="choose">
			<view class="login-m">
				<view class="login-item">
					<view class="logo">
						<open-data type="userAvatarUrl"></open-data>
					</view>
				</view>
				<view class="login-tip">
					<view class="u-tips-color">
						申请获取以下权限
					</view>
					<view class="u-tips-color">
						获得你的公开信息 （昵称、头像等）
					</view>
				</view>
			</view>
			<view class="flex justify-between" style="padding-top: 60rpx;">
				<button class="cu-btn round bg-grey flex-sub" @click="handleRefuse">拒绝</button>
				<button class="cu-btn round bg-mauve margin-left flex-sub" @click="getUserInfo">允许</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		onLoad() {

		},
		methods: {
			getCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						success: function(res) {
							if (res.code) {
								resolve(res.code)
							} else {
								//login成功，但是没有取到code
								this.$cu.toast('未取得code');
								reject(false)
							}
						},
						fail: function(res) {
							this.$cu.toast('用户授权失败wx.login');
							reject(false)
						}
					})
				})
			},
			handleRefuse() {
				uni.showToast({
					title: '未授权',
					icon: 'none',
					duration: 1000,
				})
				setTimeout(() => {
					uni.hideToast();
					uni.navigateBack(-1);
				}, 1000);
			},
			getUserInfo(e) {
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: async (res) => {
						let code = await this.getCode()
						this.$api.wxMiniLogin({
							code: code,
							userInfo: res.userInfo
						}).then(res => {
							if (!res.code) {
								this.success(res);
							}
							this.$cu.toast(res.message);
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.choose {
		background-color: #fff;
		padding: 150rpx 60rpx 0;

		.login-item {
			display: flex;
			justify-content: center;
			padding-bottom: 40rpx;
			border-bottom: 1rpx solid #dddddd;
		}

		.logo {
			display: block;
			width: 180rpx;
			height: 180rpx;
			border-radius: 50%;
			overflow: hidden;
			border: 2px solid #fff;
			box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
		}

		.login-tip {
			padding: 60rpx 0;
		}
	}
</style>
